//
//  InputContactCell.swift
//  旅航ios
//
//  Created by tulip on 2021/5/7.
//

import UIKit
import LXCountryCallingCodePicker

class InputContactCell: UITableViewCell,UITextFieldDelegate {
    var propPhoneNum:((_ str:String)->Void)!
    var propNameStr:((_ str:String)->Void)!
    @IBOutlet weak var img: UIImageView!
    var picker:CallingCodePickerController!
    
    @IBOutlet weak var name: UITextField!
    @IBOutlet weak var phone: UITextField!
    @IBOutlet weak var lab: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.backgroundColor=UIColor.clear
        self.selectionStyle = .none
        
        phone.keyboardType = .numberPad
        phone.delegate=self
        name.delegate=self
        
        lab.isUserInteractionEnabled=true
        img.isUserInteractionEnabled=true
        let tap=UITapGestureRecognizer.init()
        tap.addTarget(self, action: #selector(selectQH))
        lab.addGestureRecognizer(tap)
        img.addGestureRecognizer(tap)
        
        CallingCodeMetadataStore.language = .chinese
        picker = CallingCodePickerController()
        picker.navigationBar.tintColor = .black
        picker.title = "选择国际区号"
        picker.didSelectMetadata = { [weak self] metadata in
            guard let self = self else { return }
            
            self.lab.text = metadata.regionName + " " + metadata.callingCode
            self.picker.dismiss(animated: true, completion: nil)
        }
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    @objc func selectQH(){
        
        getCurrentVc().navigationController?.present(picker, animated: true, completion: nil)
        
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        
        if propPhoneNum != nil && textField == phone{
            propPhoneNum!(textField.text!)
        }
        
        if propNameStr != nil && textField == name{
            propNameStr!(textField.text!)
        }
        
        
    }
    
}
